import time
import pymongo

start_time = time.time()
db = pymongo.MongoClient().chapter_8
batch = db.batch
new_collection = db.update_by_insert  # 初始化2个连接，指向batch 和 update_by_insert
new_people_info_list = []
for row in batch.find():
    # print(row) # {'_id': ObjectId('6669719d42b9f371c9055cb2'), 'work_number': '1', 'name': '赵天对', 'age': '93', 'salary': '14423', 'phone': '15755324153'}
    salary = int(row['salary'])
    new_salary = salary + 100
    row['salary'] = str(new_salary)
    new_people_info_list.append(row)  # 更新之后的数据添加到新的列表
new_collection.insert_many(new_people_info_list)  # 新的列表批量插入数据库
end_time = time.time()
print('使用插入代替更新，耗时：', end_time - start_time)
# 使用插入代替更新，耗时： 4.116307735443115
